﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Collections.Specialized;
using System.IO;

////////////
/// 
/// Duplicated to System.Net.Mail
/// By marstone@marstone.net, 2008/12/02
/// 
////////////
namespace Studio.Network.Mail
{
    public class Email
    {
        EmailAddress _from;
        IList<EmailAddress> _to;
        IList<EmailAddress> _bcc;
        IList<EmailAddress> _cc;
        EmailAddress _sender;
        EmailAddress _replyTo;
        EmailPriority _priority;
        string _subject;
        Encoding _subjectEncoding = Encoding.Default;
        string _body;
        Encoding _bodyEncoding = Encoding.Default;
        string _header;

        public EmailAddress Sender
        {
            get { return _sender; }
            set { _sender = value; }
        }

        public EmailAddress ReplyTo
        {
            get { return _replyTo; }
            set { _replyTo = value; }
        }

        public string Header
        {
            get { return _header; }
            set { _header = value; }
        }

        public EmailAddress From
        {
            get { return _from; }
            set { _from = value; }
        }

        public IList<EmailAddress> To
        {
            get { return _to; }
            set { _to = value; }
        }

        public IList<EmailAddress> CC
        {
            get { return _cc; }
            set { _cc = value; }
        }

        public IList<EmailAddress> Bcc
        {
            get { return _bcc; }
            set { _bcc = value; }
        }

        public EmailPriority Priority
        {
            get { return _priority; }
            set { _priority = value; }
        }

        public string Subject
        {
            get { return _subject; }
            set { _subject = value; }
        }

        public string Body
        {
            get { return _body; }
            set { _body = value; }
        }
        
        /// <summary>
        /// Empty Constructor
        /// </summary>
        public Email() 
        {
            this._cc = new List<EmailAddress>();
            this._bcc = new List<EmailAddress>();
            this._to = new List<EmailAddress>();
        }

        public override string ToString()
        {
            string email = string.Empty;
            email += string.Format("MAIL FROM:{0}\r\n", _from);
            email += "RCPT TO:";
            for (int i = 0; i < _to.Count; i++ )
            {
                email += _to[i];
                if (i != _to.Count - 1) email += ",";
            }
            email += "\r\n\r\n";
            email += _body;
            return email;
        }

        public void SaveAs(string file)
        {
            FileStream stream = null;
            StreamWriter writer = null;
            try
            {
                // Create Directory if necessary
                string dir = Path.GetDirectoryName(file);
                if (false == Directory.Exists(dir)) Directory.CreateDirectory(dir);
                if (true == File.Exists(file))
                {
                    // Read only ?
                    File.SetAttributes(file, File.GetAttributes(file) & (~FileAttributes.ReadOnly));
                    // Overwrite without any suggestion
                    stream = File.Open(file, FileMode.Truncate, FileAccess.Write);
                }
                else
                    stream = File.Open(file, FileMode.OpenOrCreate, FileAccess.Write);
                writer = new StreamWriter(stream);
                writer.Write(this.ToString());
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine(e);
            }
            finally
            {
                if (null != writer) writer.Close();
                if (null != stream) stream.Close();
            }
        }
    }
}
